#encoding: utf-8
class Shipowner < BizUnit
  has_many :bls

  #查询该场站所有提单中的航线汇总
  def routes
    bls = self.bls.select("distinct route").order("route")
    routes = Array.new
    bls.each { |bl| routes << bl.route }
    routes
  end

  #查询该场站所有提单中的航向汇总
  def courses
    bls = self.bls.select("distinct course").order("course")
    courses = Array.new
    bls.each { |bl| courses << bl.course }
    courses
  end

  #查询该场站所有提单中的航次汇总
  def voyages
    logger.debug "航次汇总"
    bls = self.bls.order("voyage").select("distinct voyage")
    courses = Array.new
    bls.each { |bl| courses << bl.voyage }
    courses
  end

  #查询该场站所有提单中的场站汇总
  def cies
    logger.debug "场站汇总"
    Cy.joins("right outer join bls on biz_units.id = bls.cy_id").where("bls.shipowner_id" => self.id).group("bls.cy_id").order("biz_units.code")
  end

  #查询该场站所有提单中的港口汇总
  def ports
    logger.debug "港口汇总"
    Port.joins("right outer join bls on biz_units.id = bls.port_id").where("bls.shipowner_id" => self.id).group("bls.port_id").order("biz_units.code")
  end

  #查询该场站所有提单中的货代汇总
  def forwards
    logger.debug "货代汇总"
    Forward.joins("right outer join bls on biz_units.id = bls.forward_id").where("bls.shipowner_id" => self.id).group("bls.forward_id").order("biz_units.code")
  end


  def data_invalidate?(data)
    true
  end

  #报文解析
  def read_datagram(data_arr)
    data_arr.each do |data|
      if data_invalidate?(data)
        case data["command"].to_s
          when "create_bl" #创建提单或导入进口提单
            create_bl(data)

          when "confirm_bl" #提单确认
            confirm_bl(data)

          when "modify_bl" #提单修改
            modify_bl(data)

          when "remove_all_ctn" #全部撤舱
            remove_all_ctn(data)

          when "aboard" #上船
            aboard(data)

          when "sail" #开船
            sail(data)

          when "import_arrive_port" #进口抵港
            import_arrive_port(data)

          else
            logger.debug "wrong command!"
        end
        logger.debug "------------==---------end"
      end
    end

  end

  #创建提单或导入进口提单
  def create_bl(data)
    logger.debug "创建提单或导入进口提单"
    new_bl_map = data["bl"]
    return if Bl.find_all_by_bl_no(new_bl_map['bl_no']).count > 0
    bl = Bl.create!(:bl_no => new_bl_map['bl_no'], :status => BlStatusEnum.original.value,
                    :voyage => new_bl_map['voyage'], :route => new_bl_map['route'],
                    :course => new_bl_map['course'], :shipowner_id => self.id,
                    :forward_id => new_bl_map['forward_id'],
                    :forward_name => new_bl_map['forward_name'],
                    :cy_id => new_bl_map['cy_id'],
                    :port_id => new_bl_map['port_id'])
    if data["bl_containers"]
      bl.add_ctn(data['bl_containers'])
    end
    logger.debug "成功创建提单或导入进口提单--succeed create_bl!"
  end

  #提单确认
  def confirm_bl(data)
    logger.debug "提单确认"
    bl = Bl.find_by_bl_no(data['bl_no'])
    if bl
      bl.status_enum = :commit
      bl.save
      logger.debug "提单确认成功--succeed confirm_bl!"
    else
      logger.debug "no such bl with bl_no:'#{data['bl_no']}'"
    end
  end

  #提单修改
  def modify_bl(data)
    logger.debug "提单修改"
    new_bl_map = data['bl']
    bl = Bl.find_by_bl_no(new_bl_map['bl_no'])
    if bl
      bl.bl_no = new_bl_map['bl_no']
      bl.voyage = new_bl_map['voyage']
      bl.route = new_bl_map['route']
      bl.course = new_bl_map['course']
      bl.shipowner_id = self.id
      bl.forward_id = new_bl_map['forward_id']
      bl.forward_name = new_bl_map['forward_name']
      bl.cy_id = new_bl_map['cy_id']
      bl.port_id = new_bl_map['port_id']
      bl.bl_containers.each { |bl_ctn| bl_ctn.delete }
      if data["bl_containers"]
        bl.add_ctn(data['bl_containers'])
      end
      bl.save!
      logger.debug "提单修改成功--succeed modify_bl!"
    else
      logger.error "no such bl with bl_no:'#{data['bl_no']}'"
    end
  end

  #全部撤舱
  def remove_all_ctn(data)
    logger.debug "全部撤舱"
    bl = Bl.find_by_bl_no(data['bl_no'])
    if bl
      bl.bl_containers.each { |bl_ctn| bl_ctn.delete }
      bl.delete
      logger.debug "全部撤舱成功--succeed remove_all_ctn!"
    else
      logger.error "no such bl with bl_no:'#{datagram['bl_no']}'"
    end
  end

  #上船
  def aboard(data)
    logger.debug "上船"
    Bl.where(:voyage => data['voyage']).each { |bl| bl.aboard }
    logger.debug "上船成功--succeed aboard!"
  end

  #开船
  def sail(data)
    logger.debug "开船"
    Bl.where(:voyage => data['voyage']).each { |bl| bl.sail }
    logger.debug "开船成功--succeed sail!"
  end

  #进口抵港
  def import_arrive_port(data)
    logger.debug "进口抵港"
    Bl.where(:voyage => data['voyage']).each { |bl| bl.import_arrive_port }
    logger.debug "进口抵港成功--succeed import_arrive_port!"
  end
end